package com.ruoyi.project.jiaqin.apply.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jiaqin.apply.domain.CompanyApply;
import com.ruoyi.project.jiaqin.apply.service.ICompanyApplyService;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.companyindustry.domain.CompanyIndustry;
import com.ruoyi.project.jiaqin.companyindustry.service.ICompanyIndustryService;
import com.ruoyi.project.jiaqin.companyinfo.domain.CompanyInfo;
import com.ruoyi.project.jiaqin.companyinfo.service.ICompanyInfoService;
import com.ruoyi.project.system.member.service.IMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业申请Controller
 *
 * @author admin
 * @date 2020-12-24
 */
@Controller
@RequestMapping("/jiaqin/apply")
public class CompanyApplyController extends BaseController
{
    private String prefix = "jiaqin/apply";

    @Autowired
    private ICompanyApplyService companyApplyService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private ICompanyIndustryService companyIndustryService;

    @RequiresPermissions("jiaqin:apply:view")
    @GetMapping()
    public String apply()
    {
        return prefix + "/apply";
    }

    /**
     * 查询企业申请列表
     */
    @RequiresPermissions("jiaqin:apply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CompanyApply companyApply)
    {
        startPage();
        companyApply.setIsShow(1);
        List<CompanyApply> list = companyApplyService.selectCompanyApplyList(companyApply);
        for (CompanyApply CA : list) {
            CompanyIndustry companyIndustry = companyIndustryService.selectCompanyIndustryById(CA.getIndustry());
            if (companyIndustry != null) {
                CA.setIndustry(companyIndustry.getName());
            }
        }
        return getDataTable(list);
    }

    /**9
     * 导出企业申请列表
     */
    @RequiresPermissions("jiaqin:apply:export")
    @Log(title = "企业申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CompanyApply companyApply)
    {
        List<CompanyApply> list = companyApplyService.selectCompanyApplyList(companyApply);
        ExcelUtil<CompanyApply> util = new ExcelUtil<CompanyApply>(CompanyApply.class);
        return util.exportExcel(list, "apply");
    }

    /**
     * 新增企业申请
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存企业申请
     */
    @RequiresPermissions("jiaqin:apply:add")
    @Log(title = "企业申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CompanyApply companyApply)
    {
        return toAjax(companyApplyService.insertCompanyApply(companyApply));
    }

    /**
     * 修改企业申请
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CompanyApply companyApply = companyApplyService.selectCompanyApplyById(id);
        mmap.put("companyApply", companyApply);
        return prefix + "/edit";
    }

    /**
     * 修改保存企业申请
     */
    @RequiresPermissions("jiaqin:apply:edit")
    @Log(title = "企业申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CompanyApply companyApply)
    {
        return toAjax(companyApplyService.updateCompanyApply(companyApply));
    }

    /**
     * 删除企业申请
     */
    @RequiresPermissions("jiaqin:apply:remove")
    @Log(title = "企业申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(companyApplyService.deleteCompanyApplyByIds(ids));
    }

    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ICompanyInfoService companyInfoService;
    /**
     * 审核通过
     */
    @RequiresPermissions("jiaqin:apply:edit")
    @Log(title = "企业申请", businessType = BusinessType.UPDATE)
    @PostMapping("/examYe")
    @ResponseBody
    public AjaxResult editSave(String id)
    {
        CompanyApply companyApply = companyApplyService.selectCompanyApplyById(id);
        if(companyApply == null){
            return AjaxResult.error("申请不存在");
        }
        if(companyApply.getStatus() != 0){
            return AjaxResult.error("不可重复审核");
        }
//        Member member =memberService.selectMemberById(companyApply.getMemberId());
        Company company = companyService.selectCompanyByMemberId(companyApply.getMemberId());
        if(company != null){
            return AjaxResult.error("一个用户只能申请一个企业");
        }
        // 企业简介
        CompanyInfo companyInfo = new CompanyInfo();
        companyInfo.setCompanyId(companyApply.getMemberId());
        companyInfoService.insertCompanyInfo(companyInfo);
        //审核通过创建一个企业,申请状态更改
        return toAjax(companyApplyService.examYe(companyApply));
    }
    /**
     * 审核不通过
     */
    @RequiresPermissions("jiaqin:apply:edit")
    @Log(title = "企业申请", businessType = BusinessType.UPDATE)
    @PostMapping("/examNo")
    @ResponseBody
    public AjaxResult examNo(String id) {
        CompanyApply companyApply = companyApplyService.selectCompanyApplyById(id);
        if(companyApply == null){
            return AjaxResult.error("申请不存在");
        }
        if(companyApply.getStatus() != 0){
            return AjaxResult.error("不可重复审核");
        }
        companyApply.setStatus(2);
        return toAjax(companyApplyService.updateCompanyApply(companyApply));
    }
}
